package com.neusoft.neuiotms.dcom.repository.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.model.Priority;
import com.neusoft.neuiotms.dcom.model.QPriority;
import com.neusoft.neuiotms.dcom.repository.CustomizedPriorityRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

/**
 * @author WYS
 * @date 2020年8月18日 下午2:10:01
 */
public class CustomizedPriorityRepositoryImpl extends AbstractRepository implements CustomizedPriorityRepository {

    @Override
    public List<Priority> index(final Searchable searchable) {
        final QPriority qPriority = com.neusoft.neuiotms.dcom.model.QPriority.priority1;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<Priority> query = this.query().selectFrom(qPriority);
        query.where(where);
        return query.fetch();
    }

    @Override
    public Page<Priority> page(final Searchable searchable, final Pageable pageable) {
        final QPriority qPriority = com.neusoft.neuiotms.dcom.model.QPriority.priority1;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<Priority> query = this.query().selectFrom(qPriority);
        query.where(where);
        return super.search(query, pageable);
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {
        final BooleanBuilder where = new BooleanBuilder();
        final QPriority qPriority = com.neusoft.neuiotms.dcom.model.QPriority.priority1;
        if (!StringUtils.isEmpty(searchable.getStrValue("coverageInfluence"))) {
            where.and(super.containsStr(qPriority.coverageInfluence, searchable, "coverageInfluence"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("priority"))) {
            where.and(super.containsStr(qPriority.priority, searchable, "priority"));
        }
        if (!StringUtils.isEmpty(searchable.getStrValue("urgency"))) {
            where.and(super.containsStr(qPriority.urgency, searchable, "urgency"));
        }
        return where;
    }

    @Override
    protected Class<?> getModelClass() {
        return Priority.class;
    }

}
